home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / unix / tclUnixTime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  5.6 KB  |  237 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tclUnixTime.c --
  3.  *
  4.  *    Contains Unix specific versions of Tcl functions that
  5.  *    obtain time values from the operating system.
  6.  *
  7.  * Copyright (c) 1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) tclUnixTime.c 1.12 97/01/08 17:38:15
  13.  */
  14.  
  15. #include "tclInt.h"
  16. #include "tclPort.h"
  17.  
  18. /*
  19.  *-----------------------------------------------------------------------------
  20.  *
  21.  * TclpGetSeconds --
  22.  *
  23.  *    This procedure returns the number of seconds from the epoch.  On
  24.  *    most Unix systems the epoch is Midnight Jan 1, 1970 GMT.
  25.  *
  26.  * Results:
  27.  *    Number of seconds from the epoch.
  28.  *
  29.  * Side effects:
  30.  *    None.
  31.  *
  32.  *-----------------------------------------------------------------------------
  33.  */
  34.  
  35. unsigned long
  36. TclpGetSeconds()
  37. {
  38.     return time((time_t *) NULL);
  39. }
  40.  
  41. /*
  42.  *-----------------------------------------------------------------------------
  43.  *
  44.  * TclpGetClicks --
  45.  *
  46.  *    This procedure returns a value that represents the highest resolution
  47.  *    clock available on the system.  There are no garantees on what the
  48.  *    resolution will be.  In Tcl we will call this value a "click".  The
  49.  *    start time is also system dependant.
  50.  *
  51.  * Results:
  52.  *    Number of clicks from some start time.
  53.  *
  54.  * Side effects:
  55.  *    None.
  56.  *
  57.  *-----------------------------------------------------------------------------
  58.  */
  59.  
  60. unsigned long
  61. TclpGetClicks()
  62. {
  63.     unsigned long now;
  64. #ifdef NO_GETTOD
  65.     struct tms dummy;
  66. #else
  67.     struct timeval date;
  68.     struct timezone tz;
  69. #endif
  70.  
  71. #ifdef NO_GETTOD
  72.     now = (unsigned long) times(&dummy);
  73. #else
  74.     gettimeofday(&date, &tz);
  75.     now = date.tv_sec*1000000 + date.tv_usec;
  76. #endif
  77.  
  78.     return now;
  79. }
  80.  
  81. /*
  82.  *----------------------------------------------------------------------
  83.  *
  84.  * TclpGetTimeZone --
  85.  *
  86.  *    Determines the current timezone.  The method varies wildly
  87.  *    between different platform implementations, so its hidden in
  88.  *    this function.
  89.  *
  90.  * Results:
  91.  *    The return value is the local time zone, measured in
  92.  *    minutes away from GMT (-ve for east, +ve for west).
  93.  *
  94.  * Side effects:
  95.  *    None.
  96.  *
  97.  *----------------------------------------------------------------------
  98.  */
  99.  
  100. int
  101. TclpGetTimeZone (currentTime)
  102.     unsigned long  currentTime;
  103. {
  104.     /*
  105.      * Determine how a timezone is obtained from "struct tm".  If there is no
  106.      * time zone in this struct (very lame) then use the timezone variable.
  107.      * This is done in a way to make the timezone variable the method of last
  108.      * resort, as some systems have it in addition to a field in "struct tm".
  109.      * The gettimeofday system call can also be used to determine the time
  110.      * zone.
  111.      */
  112.     
  113. #if defined(HAVE_TM_TZADJ)
  114. #   define TCL_GOT_TIMEZONE
  115.     time_t      curTime = (time_t) currentTime;
  116.     struct tm  *timeDataPtr = localtime(&curTime);
  117.     int         timeZone;
  118.  
  119.     timeZone = timeDataPtr->tm_tzadj  / 60;
  120.     if (timeDataPtr->tm_isdst) {
  121.         timeZone += 60;
  122.     }
  123.     
  124.     return timeZone;
  125. #endif
  126.  
  127. #if defined(HAVE_TM_GMTOFF) && !defined (TCL_GOT_TIMEZONE)
  128. #   define TCL_GOT_TIMEZONE
  129.     time_t     curTime = (time_t) currentTime;
  130.     struct tm *timeDataPtr = localtime(¤tTime);
  131.     int        timeZone;
  132.  
  133.     timeZone = -(timeDataPtr->tm_gmtoff / 60);
  134.     if (timeDataPtr->tm_isdst) {
  135.         timeZone += 60;
  136.     }
  137.     
  138.     return timeZone;
  139. #endif
  140.  
  141. #if defined(USE_DELTA_FOR_TZ)
  142. #define TCL_GOT_TIMEZONE 1
  143.     /*
  144.      * This hack replaces using global var timezone or gettimeofday
  145.      * in situations where they are buggy such as on AIX when libbsd.a
  146.      * is linked in.
  147.      */
  148.  
  149.     int timeZone;
  150.     time_t tt;
  151.     struct tm *stm;
  152.     tt = 849268800L;      /*    1996-11-29 12:00:00  GMT */
  153.     stm = localtime(&tt); /* eg 1996-11-29  6:00:00  CST6CDT */
  154.     /* The calculation below assumes a max of +12 or -12 hours from GMT */
  155.     timeZone = (12 - stm->tm_hour)*60 + (0 - stm->tm_min);
  156.     return timeZone;  /* eg +360 for CST6CDT */
  157. #endif
  158.  
  159.     /*
  160.      * Must prefer timezone variable over gettimeofday, as gettimeofday does
  161.      * not return timezone information on many systems that have moved this
  162.      * information outside of the kernel.
  163.      */
  164.     
  165. #if defined(HAVE_TIMEZONE_VAR) && !defined (TCL_GOT_TIMEZONE)
  166. #   define TCL_GOT_TIMEZONE
  167.     static int setTZ = 0;
  168.     int        timeZone;
  169.  
  170.     if (!setTZ) {
  171.         tzset();
  172.         setTZ = 1;
  173.     }
  174.  
  175.     /*
  176.      * Note: this is not a typo in "timezone" below!  See tzset
  177.      * documentation for details.
  178.      */
  179.  
  180.     timeZone = timezone / 60;
  181.  
  182.     return timeZone;
  183. #endif
  184.  
  185. #if !defined(NO_GETTOD) && !defined (TCL_GOT_TIMEZONE)
  186. #   define TCL_GOT_TIMEZONE
  187.     struct timeval  tv;
  188.     struct timezone tz;
  189.     int timeZone;
  190.  
  191.     gettimeofday(&tv, &tz);
  192.     timeZone = tz.tz_minuteswest;
  193.     if (tz.tz_dsttime) {
  194.         timeZone += 60;
  195.     }
  196.     
  197.     return timeZone;
  198. #endif
  199.  
  200. #ifndef TCL_GOT_TIMEZONE
  201.     /*
  202.      * Cause compile error, we don't know how to get timezone.
  203.      */
  204.     error: autoconf did not figure out how to determine the timezone. 
  205. #endif
  206.  
  207. }
  208.  
  209. /*
  210.  *----------------------------------------------------------------------
  211.  *
  212.  * TclpGetTime --
  213.  *
  214.  *    Gets the current system time in seconds and microseconds
  215.  *    since the beginning of the epoch: 00:00 UCT, January 1, 1970.
  216.  *
  217.  * Results:
  218.  *    Returns the current time in timePtr.
  219.  *
  220.  * Side effects:
  221.  *    None.
  222.  *
  223.  *----------------------------------------------------------------------
  224.  */
  225.  
  226. void
  227. TclpGetTime(timePtr)
  228.     Tcl_Time *timePtr;        /* Location to store time information. */
  229. {
  230.     struct timeval tv;
  231.     struct timezone tz;
  232.     
  233.     (void) gettimeofday(&tv, &tz);
  234.     timePtr->sec = tv.tv_sec;
  235.     timePtr->usec = tv.tv_usec;
  236. }
  237.